import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:get/get.dart';


void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return GetMaterialApp( // 使用 GetMaterialApp
      title: 'Flutter Demo',
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
        useMaterial3: true,
      ),
      home: HomeScreen(), // 设置 HomeScreen 作为首页
    );
  }
}

class CounterController extends GetxController {
  var count = 0.obs;  // 使用.obs 将变量变成响应式的
  void increment() {
    count++;
  }
}

class CounterScreen extends StatelessWidget {
  // 每个页面内都会创建 CounterController.
  final CounterController controller = Get.put(CounterController());
  CounterScreen({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: Column(
          children: [
            Obx(() {
              // Obx 是用于监听响应式变量的组件
              return Text(
                'Counter: ${controller.count}',
                style: TextStyle(fontSize: 30),
              );
            }),

            TextButton(onPressed:Get.back,
                child: Text("Back")),

            TextButton(onPressed:controller.increment,
                child: Text("Increment")),
          ],
        ),
      ),
    );
  }
}

class HomeScreen extends StatelessWidget {
  const HomeScreen({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(title: const Text('Home Page')),
        body: Center(
          child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
              ElevatedButton(
              onPressed: () {
                Get.to(() => CounterScreen());
        },
          child: const Text("Go to Counter Page 1"),
        ),
        ElevatedButton(
          onPressed: () {
            Get.to(() => CounterScreen());
          },
          child: const Text("Go to Counter Page 2"),
        ),
        ElevatedButton(
        onPressed: () {
          Get.to(() => CounterScreen());
    },
          child: const Text("Go to Counter Page 3"),
        ),
              ],
          ),
        ),
    );
  }
}

